package my.dao;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

class RowMapperUtil {

	static void putRsFdToBeanFd(Object bean, Method setMd, ResultSet rs, String fdName) throws IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, SQLException, RuntimeException {

		Class<?> paramType = setMd.getParameterTypes()[0];

		setMd.invoke(bean, fieldValue(paramType, rs, fdName));

	}

	@SuppressWarnings("unchecked")
	static <T> T fieldValue(Class<T> fdType, ResultSet rs, String fdName) throws SQLException {
		BigDecimal num = null;

		if (fdType == String.class) {
			return (T) rs.getString(fdName);
		} else if (fdType == int.class) {
			return (T) new Integer(rs.getInt(fdName));
		} else if (fdType == Integer.class) {
			num = rs.getBigDecimal(fdName);
			return num == null ? null : (T) new Integer(num.intValue());
		} else if (fdType == double.class) {
			return (T) new Double(rs.getDouble(fdName));
		} else if (fdType == Double.class) {
			num = rs.getBigDecimal(fdName);
			return num == null ? null : (T) new Double(num.doubleValue());
		} else if (fdType == float.class) {
			return (T) new Float(rs.getFloat(fdName));
		} else if (fdType == Float.class) {
			num = rs.getBigDecimal(fdName);
			return num == null ? null : (T) new Float(num.floatValue());
		} else if (fdType == long.class) {
			return (T) new Long(rs.getLong(fdName));
		} else if (fdType == Long.class) {
			return num == null ? null : (T) new Long(num.longValue());
		} else if (fdType == boolean.class) {
			return (T) new Boolean(rs.getBoolean(fdName));
		} else if (fdType == Boolean.class) {
			Boolean bool = rs.getBoolean(fdName);
			return bool ? (T) Boolean.TRUE : (rs.getObject(fdName) == null ? null : (T) Boolean.FALSE);
		} else if (fdType == java.util.Date.class) {
			return (T) rs.getTimestamp(fdName);
		} else if (fdType == java.sql.Date.class) {
			return (T) rs.getDate(fdName);
		} else if (fdType == java.sql.Timestamp.class) {
			return (T) rs.getTimestamp(fdName);
		} else if (fdType == java.sql.Time.class) {
			return (T) rs.getTime(fdName);
		} else if (fdType == short.class) {
			return (T) new Short(rs.getShort(fdName));
		} else if (fdType == Short.class) {
			num = rs.getBigDecimal(fdName);
			return num == null ? null : (T) new Short(num.shortValue());
		} else if (fdType == byte.class) {
			return (T) new Byte(rs.getByte(fdName));
		} else if (fdType == Byte.class) {
			num = rs.getBigDecimal(fdName);
			return num == null ? null : (T) new Byte(num.byteValue());
		} else if (fdType == Object.class) {
			return (T) rs.getObject(fdName);
		} else {
			throw new RuntimeException("目前Dao尚不支持的bean属性的类型：" + fdType.getName());
		}
	}

	@SuppressWarnings("unchecked")
	static <T> T fieldValue(Class<T> fdType, ResultSet rs, int col) throws SQLException {
		BigDecimal num = null;

		if (fdType == String.class) {
			return (T) rs.getString(col);
		} else if (fdType == int.class) {
			return (T) new Integer(rs.getInt(col));
		} else if (fdType == Integer.class) {
			num = rs.getBigDecimal(col);
			return num == null ? null : (T) new Integer(num.intValue());
		} else if (fdType == double.class) {
			return (T) new Double(rs.getDouble(col));
		} else if (fdType == Double.class) {
			num = rs.getBigDecimal(col);
			return num == null ? null : (T) new Double(num.doubleValue());
		} else if (fdType == float.class) {
			return (T) new Float(rs.getFloat(col));
		} else if (fdType == Float.class) {
			num = rs.getBigDecimal(col);
			return num == null ? null : (T) new Float(num.floatValue());
		} else if (fdType == long.class) {
			return (T) new Long(rs.getLong(col));
		} else if (fdType == Long.class) {
			return num == null ? null : (T) new Long(num.longValue());
		} else if (fdType == boolean.class) {
			return (T) new Boolean(rs.getBoolean(col));
		} else if (fdType == Boolean.class) {
			Boolean bool = rs.getBoolean(col);
			return bool ? (T) Boolean.TRUE : (rs.getObject(col) == null ? null : (T) Boolean.FALSE);
		} else if (fdType == java.util.Date.class) {
			return (T) rs.getTimestamp(col);
		} else if (fdType == java.sql.Date.class) {
			return (T) rs.getDate(col);
		} else if (fdType == java.sql.Timestamp.class) {
			return (T) rs.getTimestamp(col);
		} else if (fdType == java.sql.Time.class) {
			return (T) rs.getTime(col);
		} else if (fdType == short.class) {
			return (T) new Short(rs.getShort(col));
		} else if (fdType == Short.class) {
			num = rs.getBigDecimal(col);
			return num == null ? null : (T) new Short(num.shortValue());
		} else if (fdType == byte.class) {
			return (T) new Byte(rs.getByte(col));
		} else if (fdType == Byte.class) {
			num = rs.getBigDecimal(col);
			return num == null ? null : (T) new Byte(num.byteValue());
		} else if (fdType == Object.class) {
			return (T) rs.getObject(col);
		} else {
			throw new RuntimeException("目前Dao尚不支持的bean属性的类型：" + fdType.getName());
		}
	}

}
